package ws.jdbc.concurrent.driver.sqlserver.cursor.individual;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.concurrent.locks.ReentrantLock;
import ws.jdbc.concurrent.driver.sqlserver.CResultSet;
import ws.jdbc.concurrent.driver.sqlserver.Messages;
import ws.jdbc.concurrent.driver.sqlserver.tds.TDSMessage;
import static ws.jdbc.concurrent.driver.sqlserver.tds.rpc.StoredProcedures.*;

/**
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class CursorForwardOnlyReadOnly extends CursorIndividualCache {

  public CursorForwardOnlyReadOnly(final TDSMessage message, final CResultSet crs, final ReentrantLock lock) throws SQLException {
    super(message, crs, lock);
  }

  @Override
  public void refreshRow() throws SQLException {
    lock.lock();
    try {
      tdsResultSet = message.cursorFetch(tdsResultSet.getCursor(), SP_CURSORFETCH_FETCHTYPE_REFRESH, 0, 0); // the last two parameters are ignored by the REFRESH fetch.
    } finally {
      lock.unlock();
    }
  }

  @Override
  public boolean next() throws SQLException {
    ++currentRow;
    if (tdsResultSet.isEmpty() || !inCache()) {
      try {
        lock.lock();
        tdsResultSet = message.cursorFetch(tdsResultSet.getCursor(), SP_CURSORFETCH_FETCHTYPE_NEXT, currentRow, fetchSize);
      } finally {
        lock.unlock();
      }
      rsIndex = 0;
      forwardCache();
    } else {
      ++rsIndex;
    }
    if (tdsResultSet.isEmpty()) {
      currentRow = ROW_AFTER;
      return false;
    } else {
      return true;
    }
  }

  // <editor-fold defaultstate="collapsed" desc="Operations not supported by forward-only result set.">
  @Override
  public boolean previous() throws SQLException {
    throw new SQLException(Messages.msg_forward_only);
  }

  @Override
  public boolean absolute(int row) throws SQLException {
    throw new SQLException(Messages.msg_forward_only);
  }

  @Override
  public boolean relative(int rows) throws SQLException {
    throw new SQLException(Messages.msg_forward_only);
  }

  @Override
  public void afterLast() throws SQLException {
    throw new SQLException(Messages.msg_forward_only);
  }

  @Override
  public void beforeFirst() throws SQLException {
    throw new SQLException(Messages.msg_forward_only);
  }

  @Override
  public boolean first() throws SQLException {
    throw new SQLException(Messages.msg_forward_only);
  }

  @Override
  public boolean last() throws SQLException {
    throw new SQLException(Messages.msg_forward_only);
  }

  @Override
  public boolean isAfterLast() throws SQLException {
    throw new SQLFeatureNotSupportedException(Messages.msg_forward_only);
  }

  @Override
  public boolean isBeforeFirst() throws SQLException {
    throw new SQLFeatureNotSupportedException(Messages.msg_forward_only);
  }

  @Override
  public boolean isFirst() throws SQLException {
    throw new SQLFeatureNotSupportedException(Messages.msg_forward_only);
  }

  @Override
  public boolean isLast() throws SQLException {
    throw new SQLFeatureNotSupportedException(Messages.msg_forward_only);
  }
  // </editor-fold>

  // <editor-fold defaultstate="collapsed" desc="Operations not supported by read-only result set.">
  @Override
  public void cancelRowUpdates() throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void moveToCurrentRow() throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void moveToInsertRow() throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void updateDate(int columnIndex, Date x) throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void updateDouble(int columnIndex, double x) throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void updateInt(int columnIndex, int x) throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void updateString(int columnIndex, String x) throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void insertRow() throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void updateRow() throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public void deleteRow() throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }

  @Override
  public boolean rowDeleted() throws SQLException {
    throw new SQLException(Messages.msg_read_only);
  }
  // </editor-fold>
}
